package cq.zhcqk.activity;

import android.content.Intent;
import android.support.v7.widget.GridLayoutManager;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.dou361.dialogui.DialogUIUtils;
import com.loopj.android.http.JsonHttpResponseHandler;
import com.loopj.android.http.RequestParams;
//import com.mrw.wzmrecyclerview.Divider.BaseItemDecoration;
//import com.mrw.wzmrecyclerview.HeaderAndFooter.OnItemClickListener;
//import com.mrw.wzmrecyclerview.HeaderAndFooter.OnItemLongClickListener;
//import com.mrw.wzmrecyclerview.PullToLoad.OnLoadListener;
//import com.mrw.wzmrecyclerview.PullToLoad.PullToLoadRecyclerView;
//import com.mrw.wzmrecyclerview.PullToRefresh.OnRefreshListener;
//import com.mrw.wzmrecyclerview.SimpleAdapter.SimpleAdapter;
//import com.mrw.wzmrecyclerview.SimpleAdapter.ViewHolder;
import com.jimi_wu.ptlrecyclerview.Divider.BaseItemDecoration;
import com.jimi_wu.ptlrecyclerview.HeaderAndFooter.OnItemClickListener;
import com.jimi_wu.ptlrecyclerview.HeaderAndFooter.OnItemLongClickListener;
import com.jimi_wu.ptlrecyclerview.PullToLoad.OnLoadListener;
import com.jimi_wu.ptlrecyclerview.PullToLoad.PullToLoadRecyclerView;
import com.jimi_wu.ptlrecyclerview.PullToRefresh.OnRefreshListener;
import com.jimi_wu.ptlrecyclerview.SimpleAdapter.SimpleAdapter;
import com.jimi_wu.ptlrecyclerview.SimpleAdapter.ViewHolder;
import com.nostra13.universalimageloader.core.ImageLoader;

import org.apache.http.Header;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.Map;

import cq.zhcqk.R;
import cq.zhcqk.base.BaseActivity;
import cq.zhcqk.util.Common;
import cq.zhcqk.util.GsonUtil;
import cq.zhcqk.util.ImageLoaderHelper;
import cq.zhcqk.util.StringUtil;

/**
 * Created by zhangle on 2017/12/11.
 */
public class SQ_SwzlActivity extends BaseActivity implements View.OnClickListener{
    private PullToLoadRecyclerView rcv;
    private ArrayList<Map<String, Object>> list;
    private int page = 1;
    private String type = "16";

    @Override
    protected void initView() {
        setContentView(R.layout.activity_sq_swzl);
        initBack();
        initRecyclerView();
    }

    @Override
    protected void initData() {
        getDetail(page);
    }
    private void initBack(){
        ImageView back = (ImageView) findViewById(R.id.header_back);
        TextView title = (TextView) findViewById(R.id.header_text);
        title.setText("失物招领");
        back.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.header_back:
                finish();
                break;
        }
    }

    private void initRecyclerView(){
        rcv = (PullToLoadRecyclerView)findViewById(R.id.rcv);

        rcv.setLayoutManager(new GridLayoutManager(context,1));
//        设置适配器，封装后的适配器只需要实现一个函数
        list = new ArrayList<>();
        rcv.setAdapter(new SimpleAdapter<Map<String, Object>>(context, list, R.layout.item_yjjy) {
            @Override
            protected void onBindViewHolder(ViewHolder holder, Map<String, Object> data) {
                String allowPing = data.get("allowPing")+"";
                if("0.0".equals(allowPing)){
                    holder.<ImageView>getView(R.id.item_header_img).setVisibility(View.VISIBLE);
                    holder.<ImageView>getView(R.id.item_content_img).setVisibility(View.GONE);
                    ImageLoader.getInstance().displayImage(StringUtil.checkImgeUrl(data.get("categories")+""), holder.<ImageView>getView(R.id.item_header_img), ImageLoaderHelper.getOptionsDf());
                }else if("2.0".equals(allowPing)){
                    holder.<ImageView>getView(R.id.item_header_img).setVisibility(View.GONE);
                    holder.<ImageView>getView(R.id.item_content_img).setVisibility(View.GONE);
                }else if("3.0".equals(allowPing)){
                    holder.<ImageView>getView(R.id.item_header_img).setVisibility(View.GONE);
                    holder.<ImageView>getView(R.id.item_content_img).setVisibility(View.VISIBLE);
                    ImageLoader.getInstance().displayImage(StringUtil.checkImgeUrl(data.get("categories")+""), holder.<ImageView>getView(R.id.item_content_img), ImageLoaderHelper.getOptionsDf());
                }
                holder.<TextView>getView(R.id.item_title).setText(""+data.get("title"));
                holder.<TextView>getView(R.id.item_num).setText(""+ StringUtil.stringToInt(data.get("hits")+""));
                holder.<TextView>getView(R.id.item_time).setText(""+data.get("time"));
            }
        });
//        设置刷新监听
        rcv.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onStartRefreshing() {
            }
        });
//        设置加载监听
        rcv.setOnLoadListener(new OnLoadListener() {
            @Override
            public void onStartLoading(int skip) {
                getDetail(page);
            }
        });
//        设置分割线
        rcv.addItemDecoration(new BaseItemDecoration(context,R.color.gray_bg));

        rcv.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void OnItemClick(int position) {
                Intent intent = new Intent(context, ListDetailSampleActivity.class);
                intent.putExtra("cid",(int) Double.parseDouble(String.valueOf(list.get(position).get("cid")))+"");
                startActivity(intent);
                overridePendingTransition(R.anim.in_from_right, R.anim.out_to_left);
            }
        });
        rcv.setOnItemLongClickListener(new OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(int position) {
                return true;
            }
        });
    }

    private void getDetail(int offset){
        RequestParams params = new RequestParams();
        params.add("limit", Common.PAGE_SIZE);
        params.add("offset",offset+"");
        params.add("type",type);
        httpClient.post(Common.URL+Common.NEWS_LIST,params,new JsonHttpResponseHandler(){
            @Override
            public void onSuccess(int statusCode, Header[] headers,
                                  JSONObject response) {
                super.onSuccess(statusCode, headers, response);
                System.out.println("onSuccess："+response);
                Map<String, Object> map = GsonUtil.GsonToMaps(response+"");
                ArrayList<Map<String, Object>> listNew = null;
                listNew = (ArrayList<Map<String, Object>>)(((Map<String, Object>)map.get("data")).get("rows"));
                if(listNew == null || listNew.size()==0){
                    Toast.makeText(context,"没有更多",Toast.LENGTH_SHORT).show();
                }else{
                    list.addAll(listNew);
                }
                rcv.completeRefresh();
                rcv.completeLoad(1);
            }

            @Override
            public void onFailure(int statusCode, Header[] headers,
                                  Throwable throwable, JSONObject errorResponse) {
                super.onFailure(statusCode, headers, throwable,
                        errorResponse);
                System.out.println("errorResponse："+errorResponse);
                Toast.makeText(context,"获取失败，请稍后再试",Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
                super.onFailure(statusCode, headers, responseString, throwable);
                System.out.println("responseString："+responseString);
                Toast.makeText(context,"获取失败，请稍后再试",Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onFinish() {
                super.onFinish();
                DialogUIUtils.dismiss(buildBean);
            }
        });
    }
}
